package code

//常用的几个算法

//冒泡排序
func PoSort(a []int) []int {
	length := len(a)

	for i := 1; i < length; i++ {
		for k := 0; k < length - i; k++ {
			if a[k] > a[k + 1] {
				tmp := a[k + 1]
				a[k + 1] = a[k]
				a[k] = tmp
			}
		}
	}
	return a
}

//快速排序法
func QuickSort(a []int) []int {
	length := len(a)
	if length <= 1 {
		return a
	}
	flag := a[0]
	leftSort := []int{}
	rightSort := []int{}
	for i := 1; i < length; i++ {
		if a[i] > flag {
			rightSort = append(rightSort, a[i])
		} else {
			leftSort = append(leftSort, a[i])
		}
	}
	leftSort = QuickSort(leftSort)
	rightSort = QuickSort(rightSort)
	leftSort = append(leftSort, flag)
	for _, v := range rightSort {
		leftSort = append(leftSort, v)
	}
	return leftSort

}

//插入排序
func InsertSort(a []int) []int {
	length := len(a)
	if length == 1 {
		return a
	}
	for i := 1; i < length; i++ {
		tmp := a[i]
		for j := i - 1; j >= 0; j-- {
			if tmp < a[j] {
				a[j + 1] = a[j]
				a[j] = tmp
			}
		}
	}
	return a
}